clc;clear;close all;addpaths;
%% =================以下开始对比4种算法的实验=================================
Area = [1850,1850+500-1,1280,1280+500-1];
load('mask.mat');
load("Figure\595348\refunwarpphase.mat");
refunwarpphase = refunwarpphase(Area(1):Area(2),Area(3):Area(4)).*MASK;
B = linspace(50,120,8);
N = linspace(3,10,8);
F = linspace(10,80,8);
X = [B;N;F];
strX = ['B','N','F'];
for i = 1:size(X,1)-2
    for j = 1:size(X,2)
        filePath = ['Figure\',strX(i),'\',strX(i),'=',int2str(X(i,j)),'\Pic_'];
        I = readFigure(filePath);
        for f = 1:length(I)
            I{f,1} = I{f,1}.*MASK;
        end
        iters = 1;
        % ========================LS=========================
%         [time_LS,Phi_LS] = main_AIA(I, iters);
%         savePath = ['Data\',strX(i),'\',strX(i),'=',int2str(X(i,j)),'\LS'];
%         saveFile(refunwarpphase, Phi_LS, time_LS, savePath, MASK);
        % ========================WLS=========================
%         savePath = ['Data\',strX(i),'\',strX(i),'=',int2str(X(i,j)),'\WLS'];
%         load([savePath,'\Sigma.mat']);
%         W = 1./Sigma.^2; W = W ./ sum(W); 
%         [time_WLS, Phi_WLS] = main_WAIA(I, iters, W);
%         saveFile(refunwarpphase, Phi_WLS, time_WLS, savePath, MASK);
        % ========================WLS(Zhang)=========================
%         savePath = ['Data\',strX(i),'\',strX(i),'=',int2str(X(i,j)),'\WLS(Zhang)'];
%         tic;
%             [rows,cols] = size(I{1,1});
%             k = estimateK(rows, cols);
%             Sigma = zeros(1, length(I));
%             for f = 1:length(I)
%                 Sigma(f) = Zhang(I{f,1}, k)/256;
%             end
%             save([savePath,'\Sigma.mat'],"Sigma");
%             W = 1./(Sigma.^2);W = W/sum(W);
%         Time = toc;
%         save([savePath,'\noiseTime.mat'],"Time");
%         [time_WLS_Zhang, Phi_WLS_Zhang] = main_WAIA(I, iters, W);
%         saveFile(refunwarpphase, Phi_WLS_Zhang, time_WLS_Zhang, savePath, MASK);
        % ========================WPCA=========================
        [time_WPCA, Phi_WPCA, W] = main_WPCA(I);
        savePath = ['Data\',strX(i),'\',strX(i),'=',int2str(X(i,j)),'\WPCA'];
        save([savePath,'\W.mat'],"W");
        saveFile(refunwarpphase, Phi_WPCA, time_WPCA, savePath, MASK);
    end
end
% for j = 1:8
%     Area = [900,1000-1+j*100,1600,1700-1+j*100];
%     experimentName = ['Experiment (',int2str(i),')'];
%     dataName = ['Data (',int2str(j),')'];
%     filePath = ['Figure/Experiment/', experimentName, '/Figure (', int2str(j), ')/Pic_'];
%     I = readFigure(filePath);
%     [I_rows, I_cols] = size(I{1,1});
%     iters = 1;
%     F = length(I);
    %=======================   1.LS    =================================================
%         algorithmsName = 'LS';
%         time_LS = main_AIA(I, iters);

    %=======================   2.WLS    =================================================
%             algorithmsName = 'WLS';
%             W = load(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/estimation.mat']);
%             W = W.estimation;W = 1./(W.^2);W = W/sum(W(:));
%             save(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/W.mat'],"W");
%             time_WLS = main_WAIA(I, experimentName, dataName, algorithmsName, iters, Area, W);
    %=======================   3.WLS(Zhang)    =================================================
%         tic;
%             algorithmsName = 'WLS(Zhang)';
%             k = estimateK(I_rows, I_cols);
%             sigmaTotal = zeros(1,F);
%             for f = 1:F
%                 sigmaTotal(f) = Zhang(I{f,1}, k)/256;
%             end
%             save(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/sigmaTotal.mat'],"sigmaTotal");
%             W = 1./(sigmaTotal.^2);W = W/sum(W(:));
%             save(['Data/', experimentName, '/', dataName,'/',algorithmsName,'/W.mat'],"W");
%         time_WLS_Zhang_noiseEstimation = toc;
%         time_WLS_Zhang = main_WAIA(I, experimentName, dataName, algorithmsName, iters, Area, W);
    %=======================   4.WPCA    =================================================
%             algorithmsName = 'WPCA';
%         time_WPCA = main_WPCA(I, experimentName, dataName, algorithmsName, Area);
%         %=======================   5.LS(Gavish)    =================================================
%         tic;
%         for f = 1:F
%             [U,D,V] = svd(I{f,1}); 
%             y = diag(D); 
%             y( y < (optimal_SVHT_coef(1,0) * median(y)) ) = 0; 
%             I{f,1} = U * diag(y) * V';
%         end
%         algorithmsName = 'LS(Gavish)';
%         main_AIA(I, experimentName, dataName, algorithmsName, iters, Area);
%         time_LS_Gavish = toc;
    %=======================    saveTime    ================================
%     TIME = [time_LS, time_WLS, time_WLS_Zhang, time_WPCA, time_WLS_Zhang_noiseEstimation];
%     save(['Data/', experimentName, '/', dataName,'/TIME.mat'],"TIME");
% end
function addpaths
    addpath(genpath('..\Algorithm'))
    addpath(genpath('..\Algorithm\GoldsteinUnwrap2D_r1'))
end